<?php
class News_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function getRelate($news_relate)
	{
		$dataResult = array();
		$count = 0;
		$dataArr = json_decode($news_relate);
		if(!empty($dataArr)) foreach($dataArr as $key => $value):
			switch($value->module):
				case 'Album':
					$is_exist = $this->db->from('album_info')->where(array('album_id' => $value->value))->count_all_results();
					if(!empty($is_exist)):
						$dataResult[$count] = $this->db->select('album_id as id,album_name as name,album_file_name as file_name,album_file_ext as file_ext')
						->get_where('album_info',array('album_id' => $value->value),1)
						->row();
						$dataArtist = $this->db->select('k.artist_id,k.artist_name')
						->join('song_artist as j','j.song_id=t.song_id','INNER')
						->join('artist_info as k','k.artist_id=j.artist_id','INNER')
						->get_where('song_album as t',array('t.album_id' => $value->value,'t.is_default' => '1'),1)
						->row();
						$dataResult[$count]->module = 'album';
						$dataResult[$count]->watermark = '';
						$dataResult[$count]->link = $this->config->item('base_artist_url').'album/'.$dataArtist->artist_id.'-'.seo_url($dataArtist->artist_name).'/'.$dataResult[$count]->id.'-'.seo_url($dataResult[$count]->name);
						$count++;
					endif;
				break;
				case 'Movie':
					$is_exist = $this->db->from('movie_info')->where(array('movie_id' => $value->value))->count_all_results();
					if(!empty($is_exist)):
						$dataResult[$count] = $this->db->select('movie_id as id,movie_name as name')
						->get_where('movie_info',array('movie_id' => $value->value),1)
						->row();
						$dataImage = $this->db->order_by('movie_image_order','ASC')->get_where('movie_image',array('movie_id' => $value->value),1)->row();
						$dataResult[$count]->file_name = @$dataImage->movie_file_name;
						$dataResult[$count]->file_ext = @$dataImage->movie_file_ext;
						$dataResult[$count]->module = 'movie';
						$dataResult[$count]->watermark = 'watermark_video.png';
						$dataResult[$count]->link = $this->config->item('base_movie_url').'play/'.$dataResult[$count]->id.'-'.seo_url($dataResult[$count]->name);
						$count++;
					endif;		
				break;
				case 'Song':
					$is_exist = $this->db->from('song_info')->where(array('song_id' => $value->value))->count_all_results();
					if(!empty($is_exist)):
						$dataResult[$count] = $this->db->select('t.song_id as id,t.song_name as name,k.album_file_name as file_name,k.album_file_ext as file_ext')
						->join('song_album as j',"j.song_id=t.song_id AND is_default='1'",'INNER')
						->join('album_info as k','k.album_id=j.album_id','LEFT')
						->get_where('song_info as t',array('t.song_id' => $value->value),1)
						->row();
						
						$dataResult[$count]->module = 'album';
						$dataResult[$count]->watermark = 'watermark_music.png';
						$dataResult[$count]->link = $this->config->item('base_music_url').'play/'.$dataResult[$count]->id.'-'.seo_url($dataResult[$count]->name);
						$count++;
					endif;					
				break;
				case 'Video':
					$is_exist = $this->db->from('video_info')->where(array('video_id' => $value->value))->count_all_results();
					if(!empty($is_exist)):
						$dataResult[$count] = $this->db->select('video_id as id,video_name as name,video_thumb_name as file_name,video_thumb_ext as file_ext')
						->get_where('video_info',array('video_id' => $value->value),1)
						->row();
						
						$dataResult[$count]->module = 'video';
						$dataResult[$count]->watermark = 'watermark_video.png';
						$dataResult[$count]->link = $this->config->item('base_video_url').'play/'.$dataResult[$count]->id.'-'.seo_url($dataResult[$count]->name);
						$count++;
					endif;				
				break;
				case 'Gallery':
					$is_exist = $this->db->from('gallery')->where(array('gallery_id' => $value->value))->count_all_results();
					if(!empty($is_exist)):
						$dataResult[$count] = $this->db->select('gallery_id as id,gallery_name as name')
						->get_where('gallery',array('gallery_id' => $value->value),1)
						->row();
						$dataImage = $this->db->order_by("gallery_image_order ASC,gallery_image_id DESC")->get_where('gallery_image',array('gallery_id' => $value->value),1)->row();
						$dataResult[$count]->file_name = @$dataImage->gallery_file_name;
						$dataResult[$count]->file_ext = @$dataImage->gallery_file_ext;
						$dataResult[$count]->module = 'gallery';
						$dataResult[$count]->watermark = 'watermark_gallery.png';
						$dataResult[$count]->link = $this->config->item('base_gallery_url').'archives/'.$dataResult[$count]->id.'-'.seo_url($dataResult[$count]->name);
						$count++;
					endif;				
				break;
				case 'Fashion':
					$is_exist = $this->db->from('fashion')->where(array('fashion_id' => $value->value))->count_all_results();
					if(!empty($is_exist)):
						$dataResult[$count] = $this->db->select('fashion_id as id,fashion_name as name')
						->get_where('fashion',array('fashion_id' => $value->value),1)
						->row();
						$dataImage = $this->db->order_by("fashion_image_order ASC,fashion_image_id DESC")->get_where('fashion_image',array('fashion_id' => $value->value),1)->row();
						$dataResult[$count]->file_name = @$dataImage->fashion_file_name;
						$dataResult[$count]->file_ext = @$dataImage->fashion_file_ext;
						$dataResult[$count]->module = 'fashion';
						$dataResult[$count]->watermark = 'watermark_gallery.png';
						$dataResult[$count]->link = $this->config->item('base_fashion_url').'archives/'.$dataResult[$count]->id.'-'.seo_url($dataResult[$count]->name);
						$count++;
					endif;				
				break;							
			endswitch;		
		endforeach;
		return $dataResult;
	}
}

?>